﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace LdCms.BLL.Community
{
    using LdCms.EF.DbEntitiesContext;
    using LdCms.EF.DbModels;
    using LdCms.EF.DbStoredProcedure;
    using LdCms.IBLL.Community;
    using LdCms.IDAL.Community;
    using LdCms.Common.Json;
    using LdCms.Common.Security;
    using LdCms.Common.Extension;
    using LdCms.Common.Utility;
    using LdCms.Common.Net;

    public partial class ThemeService:BaseService<Ld_Community_Theme>,IThemeService
    {
        private readonly IThemeDAL ThemeDAL;
        private readonly LdCmsDbEntitiesContext LdCmsDbEntitiesContext;
        public ThemeService(LdCmsDbEntitiesContext LdCmsDbEntitiesContext, IThemeDAL ThemeDAL)
        {
            this.LdCmsDbEntitiesContext = LdCmsDbEntitiesContext;
            this.ThemeDAL = ThemeDAL;
            this.Dal = ThemeDAL;
        }
        public override void SetDal()
        {
            Dal = ThemeDAL;
        }

        public bool IsExists(int systemId, string companyId, string themeId)
        {
            try
            {
                return IsExists(m => m.SystemID == systemId && m.CompanyID == companyId && m.ThemeID == themeId);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public bool SaveTheme(Ld_Community_Theme entity, Ld_Community_ThemeContent content)
        {
            try
            {
                int systemId = entity.SystemID;
                string companyId = entity.CompanyID;
                string themeId = entity.ThemeID;
                if (string.IsNullOrEmpty(themeId))
                {
                    var communityTheme = PrimaryKeyHelper.PrimaryKeyType.CommunityTheme;
                    var primaryKeyLen = PrimaryKeyHelper.PrimaryKeyLen.V3;
                    string createThemeId = PrimaryKeyHelper.MakePrimaryKey(communityTheme, primaryKeyLen);
                    entity.ThemeID = createThemeId;
                    content.ThemeID = createThemeId;
                }
                entity.Hits = entity.Hits.ToInt();
                entity.ReplyTotalNum = entity.ReplyTotalNum.ToInt();
                entity.NotVerifyTotalNum = entity.NotVerifyTotalNum.ToInt();
                entity.UpNum = entity.UpNum.ToInt();
                entity.DownNum = entity.DownNum.ToInt();
                entity.IsTop = entity.IsTop.ToBool();
                entity.IsWonderful = entity.IsWonderful.ToBool();
                entity.IsLock = entity.IsLock.ToBool();
                entity.IpAddress = Net.Ip;
                entity.State = entity.State.ToBool();
                entity.CreateDate = DateTime.Now;
                content.SystemID = systemId;
                content.CompanyID = companyId;
                content.CreateDate = DateTime.Now;

                int intnum = 0;
                var dbContext = new DAL.BaseDAL(LdCmsDbEntitiesContext);
                var db = dbContext.DbEntities();
                using (var trans = db.Database.BeginTransaction())
                {
                    try
                    {
                        dbContext.Add(entity);
                        dbContext.Add(content);
                        intnum = db.SaveChanges();
                        trans.Commit();
                    }
                    catch (Exception)
                    {
                        trans.Rollback();
                    }
                    return intnum > 0;
                }
            }
            catch(Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public bool UpdateTheme(Ld_Community_Theme entity, Ld_Community_ThemeContent content)
        {
            try
            {
                int systemId = entity.SystemID;
                string companyId = entity.CompanyID;
                string themeId = entity.ThemeID;

                entity.ThemeID = themeId;
                entity.Hits = entity.Hits.ToInt();
                entity.ReplyTotalNum = entity.ReplyTotalNum.ToInt();
                entity.NotVerifyTotalNum = entity.NotVerifyTotalNum.ToInt();
                entity.UpNum = entity.UpNum.ToInt();
                entity.DownNum = entity.DownNum.ToInt();
                entity.IsTop = entity.IsTop.ToBool();
                entity.IsWonderful = entity.IsWonderful.ToBool();
                entity.IsLock = entity.IsLock.ToBool();
                entity.IpAddress = Net.Ip;
                entity.State = entity.State.ToBool();
                entity.CreateDate = DateTime.Now;
                content.SystemID = systemId;
                content.CompanyID = companyId;
                content.ThemeID = themeId;
                content.CreateDate = DateTime.Now;

                int intnum = 0;
                var dbContext = new DAL.BaseDAL(LdCmsDbEntitiesContext);
                var db = dbContext.DbEntities();
                using (var trans = db.Database.BeginTransaction())
                {
                    try
                    {
                        dbContext.Update(entity);
                        dbContext.Update(content);
                        intnum = db.SaveChanges();
                        trans.Commit();
                    }
                    catch (Exception)
                    {
                        trans.Rollback();
                    }
                    return intnum > 0;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public bool UpdateThemeState(int systemId, string companyId, string themeId, bool state)
        {
            try
            {
                var entity = GetTheme(systemId, companyId, themeId);
                entity.State = state;
                return Update(entity);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public bool DeleteTheme(int systemId, string companyId, string themeId)
        {
            try
            {
                if (!IsExists(systemId, companyId, themeId))
                    throw new Exception("ID无效！");
                int intnum = 0;
                var dbContext = new DAL.BaseDAL(LdCmsDbEntitiesContext);
                var db = dbContext.DbEntities();
                var trans = db.Database.BeginTransaction();
                try
                {
                    dbContext.Delete<Ld_Community_ThemeReply>(m => m.SystemID == systemId && m.CompanyID == companyId && m.ThemeID == themeId);
                    dbContext.Delete<Ld_Community_ThemeContent>(m => m.SystemID == systemId && m.CompanyID == companyId && m.ThemeID == themeId);
                    dbContext.Delete<Ld_Community_Theme>(m => m.SystemID == systemId && m.CompanyID == companyId && m.ThemeID == themeId);
                    intnum = db.SaveChanges();
                    trans.Commit();
                }
                catch (Exception)
                {
                    trans.Rollback();
                }
                return intnum > 0;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public Ld_Community_Theme GetTheme(int systemId, string companyId, string themeId)
        {
            try
            {
                var expression = ExtLinq.True<Ld_Community_Theme>();
                expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId && m.ThemeID == themeId);
                return Find(expression);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public List<Ld_Community_Theme> GetThemeTop(int systemId, string companyId, int count)
        {
            try
            {
                int total = Utility.ToTopTotal(count);
                var expression = ExtLinq.True<Ld_Community_Theme>();
                expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId);
                return FindListTop(expression, m => m.CreateDate, false, total).ToList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public List<Ld_Community_Theme> GetThemePaging(int systemId, string companyId, int pageId, int pageSize)
        {
            try
            {
                var expression = ExtLinq.True<Ld_Community_Theme>();
                expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId);
                int pageIndex = Utility.ToPageIndex(pageId);
                int pageCount = Utility.ToPageCount(pageSize);
                return FindListPaging(expression, m => m.CreateDate, false, pageIndex, pageCount).ToList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public List<Ld_Community_Theme> SearchTheme(int systemId, string companyId, string startTime, string endTime, string columnId, string top,string wonderful, string state, string keyword, int count)
        {
            try
            {
                DateTime dateStartTime = Utility.ToStartTime(startTime);
                DateTime dateEndTime = Utility.ToEndTime(endTime);
                bool blnTop = top.ToBool();
                bool blnWonderful = wonderful.ToBool();
                bool blnState = state.ToBool();
                int total = Utility.ToTopTotal(count);
                //条件
                var expression = ExtLinq.True<Ld_Community_Theme>();
                expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId
                && m.CreateDate.Value.Date >= dateStartTime.Date && m.CreateDate.Value.Date <= dateEndTime.Date
                && (string.IsNullOrEmpty(columnId) ? true : m.ColumnID == columnId)
                && (string.IsNullOrEmpty(top) ? true : m.IsTop == blnTop)
                && (string.IsNullOrEmpty(wonderful) ? true : m.IsWonderful == blnWonderful)
                && (string.IsNullOrEmpty(state) ? true : m.State.Value == blnState)
                && (m.MemberID.Contains(keyword) || m.Title.Contains(keyword)));
                //执行
                return FindListTop(expression, m => m.CreateDate, false, total).ToList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public int CountTheme(int systemId, string companyId)
        {
            try
            {
                return Count(m => m.SystemID == systemId && m.CompanyID == companyId);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}
